﻿<!DOCTYPE html>
@{
    string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);
    
    string selected = "";
    selected = Request.QueryString["selected"];

    try
    {
        String filename = System.DateTime.Now.Year+"_"+System.DateTime.Now.Month+"_"+System.DateTime.Now.Day+"_"+System.DateTime.Now.Hour+System.DateTime.Now.Minute+System.DateTime.Now.Second;

        StreamWriter writer = new StreamWriter(new FileStream(Server.MapPath("~/Exports/"+filename+".txt"),FileMode.Append));

        MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand("SELECT domain_name_servers, default_lease_time, max_lease_time, authoritative, name, log_facility FROM servers WHERE name='"+selected+"';", connection);
        connection.Open();

        MySql.Data.MySqlClient.MySqlDataReader reader = command.ExecuteReader();

        reader.Read();
        System.Data.IDataRecord record = (System.Data.IDataRecord)reader;
        writer.WriteLine("#Opcje serwera\n");
        writer.Write("option domain-name-servers ");
        writer.WriteLine(record.GetString(0)+";");
        writer.Write("default-lease-time ");
        writer.WriteLine(record.GetString(1)+";");
        writer.Write("max-lease-time ");
        writer.WriteLine(record.GetString(2)+";");
        if(record.GetString(3)=="True"){writer.WriteLine("authoritative;");}
        writer.Write("log-facility ");
        writer.WriteLine(record.GetString(5)+";");
        writer.WriteLine();
        reader.Close();

        command = new MySql.Data.MySqlClient.MySqlCommand("SELECT id FROM servers WHERE name='"+selected+"';", connection);
        string server_id = Convert.ToString(command.ExecuteScalar());

        command = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM subnets WHERE server='"+server_id+"';", connection);
        reader = command.ExecuteReader();

        System.Data.DataTable subnets = new System.Data.DataTable();
        subnets.Load(reader);
        reader.Close();

        foreach(System.Data.DataRow row in subnets.Rows)
        {
            writer.WriteLine("#Budynek - "+row[7]);
            writer.WriteLine("#"+row[8]);
            writer.WriteLine();
            
            writer.WriteLine("subnet "+row[1]+" netmask "+row[2]+" {");
            if(row[3]!="")
            {
                writer.WriteLine("  range "+row[3]+";");
            }
            if(row[4]!="")
            {
                writer.WriteLine("  option broadcast-address "+row[4]+";");
            }
            if(row[5]!="")
            {
                writer.WriteLine("  option routers "+row[5]+";");
            }
            writer.WriteLine();

            // TO DO - hosty
            command = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM hosts WHERE subnet='"+row[0]+"';", connection);
            reader = command.ExecuteReader();

            System.Data.DataTable hosts = new System.Data.DataTable();
            hosts.Load(reader);
            reader.Close();

            if(hosts.Rows.Count>0)
            {
                foreach(System.Data.DataRow host in hosts.Rows)
                {
                    writer.WriteLine("  host "+host[0]+" {");
                    writer.WriteLine("    hardware ethernet "+host[1]+";");
                    writer.WriteLine("    fixed-address "+host[2]+";");
                    writer.WriteLine("  }");
                    writer.WriteLine();
                }
            }
            writer.WriteLine("}");
            writer.WriteLine();
        }

        //Zakończenie operacji
        writer.Close();
        connection.Close();
        Response.Redirect("default.cshtml");
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
        Response.Write(ex.StackTrace);
    }
}